<?php
namespace App\Controllers\Sys;

use App\Controllers\BaseController;
use App\Models\Special;
use App\Models\HdLog;
use App\Models\SmsLog;
use App\Library\Helper;

class MobileController extends BaseController {

    public function initialize() {
        parent::initialize();
        if($this->request->isPost()) {
            $this->checkLogin();
        }
    }

    //---------------------------------------------------------------------------------
    //短信内容列表
    //---------------------------------------------------------------------------------
    public function indexAction() {
        $this->display('sys/mobile/index');
    }

    //---------------------------------------------------------------------------------
    //获取指定手机号码对应的短信内容
    //---------------------------------------------------------------------------------
    public function getsAction() {
        $mobile = $this->request->getPost('mobile');
        if($mobile == '') {
            return $this->sendAjax(false, '获取数据失败，未获取到指定的查询手机号码！');
        }
        if(!Helper::isMobile($mobile)) {
            return $this->sendAjax(false, '获取数据失败，查询的手机号码格式不正确！');
        }
        $special       = new Special();
        $hdLog         = new HdLog();
        $smsLog        = new SmsLog();
        $specials      = $special->getsByMobile($mobile);
        $hdLogs        = $hdLog->getsByMobile($mobile);
        $smsLogs       = $smsLog->getsByMobile($mobile);
        $specialsArray = [];
        $hdLogsArray   = [];
        $smsLogsArray  = [];
        $count         = 0;
        foreach($specials as $k => $v) {
            $specialsArray[$count]['id']             = $v['id'];
            $specialsArray[$count]['mobile']         = $v['mobile'];
            $specialsArray[$count]['message']        = $v['message'];
            $specialsArray[$count]['mt_api_message'] = $v['mt_api_message'];
            $specialsArray[$count]['mt_status']      = $v['mt_api_message'] != '' ? ($v['mt_status'] == 200 ? "成功" : '失败') . '(' . $v['mt_status'] . ')' : '';
            $specialsArray[$count]['create_at']      = $v['create_at'];
            $specialsArray[$count]['long_code']      = $v['long_code'];
            $count++;
        }
        foreach($hdLogs as $k => $v) {
            $hdLogsArray[$count]['id']             = $v['id'];
            $hdLogsArray[$count]['mobile']         = $v['mobile'];
            $hdLogsArray[$count]['message']        = $v['message'];
            $hdLogsArray[$count]['mt_api_message'] = $v['mt_api_message'];
            $hdLogsArray[$count]['mt_status']      = $v['mt_api_message'] != '' ? ($v['mt_status'] == -1 ? "失败" : ($v['mt_status'] == 1 ? '成功' : (($v['mt_api_message'] != '' && $v['mt_status'] == 0) ? '未回执' : ''))) : '';
            $hdLogsArray[$count]['create_at']      = $v['create_at'];
            $hdLogsArray[$count]['long_code']      = $v['long_code'];
            $count++;
        }
        foreach($smsLogs as $k => $v) {
            $smsLogsArray[$count]['id']             = $v['id'];
            $smsLogsArray[$count]['mobile']         = $v['mobile'];
            $smsLogsArray[$count]['message']        = '';
            $smsLogsArray[$count]['mt_api_message'] = $v['mt'];
            $smsLogsArray[$count]['mt_status']      = $v['mt'] != '' ? ($v['status'] == 200 ? "成功" : '失败') . '(' . $v['status'] . ')' : '';
            $smsLogsArray[$count]['create_at']      = $v['create_at'];
            if($v['action'] == 'captcha') {
                $smsLogsArray[$count]['long_code'] = '验证码短信';
            } else {
                $smsLogsArray[$count]['long_code'] = '通知短信';
            }
            $count++;
        }
        $ret = array_merge($specialsArray, $hdLogsArray, $smsLogsArray);
        usort($ret, function ($v1, $v2) {
            $v1 = strtotime($v1['create_at']);
            $v2 = strtotime($v2['create_at']);
            return $v1 == $v2 ? 0 : ($v1 > $v2 ? -1 : 1);
        });
        if($ret !== false) {
            return $this->sendAjax(true, $ret);
        } else {
            return $this->sendAjax(false, "获取数据失败，系统错误！");
        }
    }
}
